package com.eliauk.demo.controller.write;

import com.alibaba.excel.EasyExcel;
import com.eliauk.demo.entity.wirte.DemoData;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/write2")
public class write2Controller {

    List<DemoData> data = ListData.demoData();

    /**
     * 只导出指定列
     * @param response
     * @throws IOException
     */
    @GetMapping("download")
    public void uploadData(HttpServletResponse response) throws IOException {
        // 根据用户传入字段 假设我们要忽略 date
        Set<String> excludeColumnFiledNames = new HashSet<String>();
        excludeColumnFiledNames.add("date");

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), DemoData.class)
                .excludeColumnFieldNames(excludeColumnFiledNames)
                .sheet("模板")
                .doWrite(data);
    }
}
